using System;
using System.Drawing.Drawing2D;
using System.IO;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Interactivity;
using Avalonia.Styling;
using CommunityToolkit.Mvvm.Messaging;
using NDY_1.ContentDialogs;
using NDY_1.Model;
using Newtonsoft.Json;
using Path = System.IO.Path;

namespace NDY_1;

public partial class MainWindow : Window
{
    private readonly string _currentLanguagePath;
    private readonly SystemSetting _systemSetting = new SystemSetting();
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowViewModel();
        _currentLanguagePath = Path.Combine(Environment.CurrentDirectory, "Assets", "SystemSetting.json");
        BtnMinimize.Click += (s, e) =>
        {
            WindowState = WindowState.Minimized;
        };
        var json = File.ReadAllText(_currentLanguagePath);
        _systemSetting = JsonConvert.DeserializeObject<SystemSetting>(json);
        Localizer.Localizer.Instance.LoadLanguage(_systemSetting.CurrentLanguage);
        switch (_systemSetting.CurrentLanguage)
        {
            case "chinese":LanguageSwitch.SelectedIndex=0;break;
            case "english":LanguageSwitch.SelectedIndex=2;break;
            default: LanguageSwitch.SelectedIndex = 1;break;
        }
        WeakReferenceMessenger.Default.Register<CloseWindowDialog>(this,ShowErrorDialog);
    }

    private async void ShowErrorDialog(object recipient, CloseWindowDialog message)
    {
        var messageDialog = new CloseWindowDialog { Message="Проверьте соединение устройства?"};
        await  messageDialog.ShowDialog<string>(this);
        if(messageDialog.Message=="Yes")
            Close();
    }

    private void ToggleButton_OnIsCheckedChanged(object? sender, RoutedEventArgs e)
    {
        var app = Application.Current;
        if (app is null) return;
        var theme = app.ActualThemeVariant;
        app.RequestedThemeVariant = theme == ThemeVariant.Dark ? ThemeVariant.Light : ThemeVariant.Dark;
    }

    private async void BtnClose_OnClick(object? sender, RoutedEventArgs e)
    {
        var messageDialog = new CloseWindowDialog { Message="Are you sure you want to exit?"};
        await  messageDialog.ShowDialog<string>(this);
        if(messageDialog.Message=="Yes")
            Close();
    }

    // private void SelectingItemsControl_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
    // {
    //     switch (LanguageComboBox.SelectedIndex)
    //     {
    //         case 0:Localizer.Localizer.Instance.LoadLanguage("chinese");
    //             break;
    //         case 1:Localizer.Localizer.Instance.LoadLanguage("english");
    //             break;
    //         case 2:Localizer.Localizer.Instance.LoadLanguage("russia");
    //             break;
    //     }
    // }
    private void LanguageSwitch_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        var combobox = (ComboBox)sender;
        if (combobox.SelectedItem == null || _systemSetting == null || _currentLanguagePath==null) return;
        switch (combobox.SelectedIndex)
        {
            case 0:Localizer.Localizer.Instance.LoadLanguage("chinese");
                _systemSetting.CurrentLanguage="chinese";
                break;
            case 2:Localizer.Localizer.Instance.LoadLanguage("english");
                _systemSetting.CurrentLanguage="english";
                break;
            default:
                Localizer.Localizer.Instance.LoadLanguage("russia");
                _systemSetting.CurrentLanguage="russia";
                break;
        }
        File.WriteAllText(_currentLanguagePath, JsonConvert.SerializeObject(_systemSetting));
    }
}